package star.cloud.asme.asmePerson.asmePersonTrain.asmePersonTrainRecord;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import star.cloud.asme.base.dto.Result;
import star.cloud.asme.base.file.fileInfo.FileInfoMapper;
import star.cloud.asme.base.mybatis.IBaseServiceImpl;

import java.time.LocalDateTime;


@Service
@Slf4j
@RequiredArgsConstructor
public class AsmePersonTrainRecordServiceImpl extends IBaseServiceImpl<AsmePersonTrainRecordMapper, AsmePersonTrainRecord> implements AsmePersonTrainRecordService {

    private final AsmePersonTrainRecordMapper asmePersonTrainRecordMapper;

    private final FileInfoMapper fileInfoMapper;

    @Override
    public Result<AsmePersonTrainRecord> getOneByCondition(String columnName, Object columnValue) {
        LambdaQueryWrapper<AsmePersonTrainRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (columnName.equals("id")) {
            queryWrapper.eq(AsmePersonTrainRecord::getId, columnValue);
        }
        AsmePersonTrainRecord asmePersonTrainCourse = asmePersonTrainRecordMapper.selectOne(queryWrapper);
        return Result.ok(asmePersonTrainCourse);
    }

    @Override
    public Result<IPage<AsmePersonTrainRecord>> getListByConditionsWithPage(AsmePersonTrainRecord conditions) {
        IPage<AsmePersonTrainRecord> iPage = new Page<>(conditions.getCurrentPage(), conditions.getPageSize());
        LambdaQueryWrapper<AsmePersonTrainRecord> queryWrapper = new LambdaQueryWrapper<>();
        asmePersonTrainRecordMapper.selectPage(iPage, queryWrapper);
        return Result.ok(iPage);
    }

    @Override
    public Result<AsmePersonTrainRecord> insertOne(AsmePersonTrainRecord entity) {
        entity.setStartTime(LocalDateTime.now());
        int inserted = asmePersonTrainRecordMapper.insert(entity);
        setCreatorAndUpdater(entity);
        return inserted > 0 ? Result.ok(entity) : Result.error();
    }

    @Override
    public Result<AsmePersonTrainRecord> updateOne(AsmePersonTrainRecord entity) {
        setVersion(entity);
        int updated = asmePersonTrainRecordMapper.updateById(entity);
        setCreatorAndUpdater(entity);
        return updated > 0 ? Result.ok(entity) : Result.error();
    }
}
